package com.dyrj.pig.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyrj.base.system.user.model.SysUser;
import com.dyrj.component.properties.UploadProperties;
import com.dyrj.constant.SysFrameConst;
import com.dyrj.pig.bean.TRecordBindcard;
import com.dyrj.pig.bean.TUser;
import com.dyrj.pig.bean.TYhk;
import com.dyrj.pig.bean.TYskBindcard;
import com.dyrj.pig.dao.TRecordBindcardMapper;
import com.dyrj.pig.dao.TUserMapper;
import com.dyrj.pig.dao.TYhkMapper;
import com.dyrj.pig.dao.TYskBindcardMapper;
import com.dyrj.pig.service.TYskBindcardService;
import com.dyrj.utils.UUIDUtil;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**   
 * @Title: TaskServiceImpl.java 
 * @Package com.dyrj.base.system.user.service.impl
 * @Description: ()
 * @author   
 * @date 2019年4月13日 下午11:30:23
 * @version V1.0   
 */
@Service
@Log4j2
public class TYskBindcardServiceImpl implements TYskBindcardService {
    //    private static SimpleDateFormat sdf_datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sdf_datetime = new SimpleDateFormat("yyyyMMddHHmmss");

    @Resource
	private TYskBindcardMapper tYskBindcardMapper;

    @Resource
    private UploadProperties uploadProperties;

    @Resource
    private TRecordBindcardMapper tRecordBindcardMapper;

    @Resource
    private TUserMapper tUserMapper;

    @Resource
    private TYhkMapper tYhkMapper;

	@Override
	public PageResultMsg table(TYskBindcard record) {
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
//		record.setUserid(loginUser.getAccount());
		PageInfo<TYskBindcard> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> tYskBindcardMapper.selectList(record));
		return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
	}


	@Override
	public ResultMsg add(TYskBindcard record) {
        String msg = "";
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
        record.setId(UUIDUtil.uuid());
        record.setInsertime(new Date());
        record.setUpdatetime(new Date());
        record.setUserid(loginUser.getAccount());

        try {
            tYskBindcardMapper.insertSelective(record);

        } catch (Exception e) {
        	e.printStackTrace();
            msg = e.getMessage();
        }

        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE+msg);
	}



	@Override
	public TYskBindcard info(String id) {
		TYskBindcard tYskBindcard = tYskBindcardMapper.selectByPrimaryKey(id);
		return tYskBindcard;
	}
	
	public ResultMsg update(TYskBindcard record) {
        String msg = "";
        try {
//            TYskBindcard record_db = tYskBindcardMapper.selectByPrimaryKey(record.getId());
            tYskBindcardMapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }

		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE + msg);
	}
	
	@Override
	public ResultMsg delete(String id) {
		tYskBindcardMapper.deleteByPrimaryKey(id);
		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
	}

	@Override
    public void bindCardNotify(Map<String,String> params){
        String TraderNo = params.get("TraderNo");
        String BankName = params.get("BankName");
        String ClientNo = params.get("ClientNo");
        String BindCardSeqNo = params.get("BindCardSeqNo");
        String BankCardNo = params.get("BankCardNo");
        String BindCardStatus = params.get("BindCardStatus");
        String BankCode = params.get("BankCode");
        String BindCardTime = params.get("BindCardTime");
        String BindCardNo = params.get("BindCardNo");
        String BankCardType = params.get("BankCardType");
        String Mobile = params.get("Mobile");

        TUser tuser = null;
        try{
            QueryWrapper<TUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("membercode",ClientNo);
            tuser = tUserMapper.selectOne(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
        }

        Date now = new Date();
        try{
            TYskBindcard tYskBindcard = new TYskBindcard();
            if(tuser!=null){
                tYskBindcard.setUserid(tuser.getYhbm().toString());
            }
            tYskBindcard.setId(UUIDUtil.uuid());
            tYskBindcard.setTraderno(TraderNo);
            tYskBindcard.setClientno(ClientNo);
            tYskBindcard.setBindcardseqno(BindCardSeqNo);
            tYskBindcard.setBindcardno(BindCardNo);
            tYskBindcard.setBindcardtime(BindCardTime);
            tYskBindcard.setBindcardstatus(BindCardStatus);
            tYskBindcard.setMobile(Mobile);
            tYskBindcard.setBankcardtype(BankCardType);
            tYskBindcard.setBankname(BankName);
            tYskBindcard.setBankcode(BankCode);
            tYskBindcard.setBankcardno(BankCardNo);
            tYskBindcard.setInsertime(now);
            tYskBindcard.setUpdatetime(now);
            tYskBindcardMapper.insertSelective(tYskBindcard);
        }catch (Exception e){
            e.printStackTrace();
        }

        try{
            TRecordBindcard tRecordBindcard = new TRecordBindcard();
            tRecordBindcard.setUserid(tuser.getYhbm().toString());
            tRecordBindcard.setXm(tuser.getXm());
            tRecordBindcard.setMobile(Mobile);
            tRecordBindcard.setBankcardtype(BankCardType);
            tRecordBindcard.setBankname(BankName);
            tRecordBindcard.setBankcode(BankCode);
            tRecordBindcard.setBindcardstatus(BindCardStatus);
            tRecordBindcard.setBindcardseqno(BindCardSeqNo);
            if(BindCardTime!=null && !"".equals(BindCardTime)){
                try{
                    tRecordBindcard.setBindcardtime(sdf_datetime.parse(BindCardTime));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            tRecordBindcardMapper.insertSelective(tRecordBindcard);

        }catch (Exception e){
            e.printStackTrace();
        }

        try{
            TYhk tYhk = new TYhk();
            tYhk.setId(UUIDUtil.uuid());
            tYhk.setYhbm(tuser.getYhbm());
            tYhk.setXm(tuser.getXm());
            tYhk.setMobile(Mobile);
            tYhk.setSfzh(tuser.getSfzh());
            tYhk.setYhkh(BankCardNo);
            tYhk.setYhklx(BankCardType);
            tYhk.setYhmc(BankName);
            tYhk.setYhdm(BankCode);
            tYhk.setBindcardno(BindCardNo);
            tYhk.setBdzt(1); //绑定状态（0：未绑定，1：绑定成功，2：绑定失败）
            tYhk.setMembercode(ClientNo);
            tYhk.setSubno(tuser.getSubno());
            tYhk.setInsertime(now);
            tYhk.setUpdatetime(now);
            tYhkMapper.insertSelective(tYhk);
        }catch (Exception e){
            e.printStackTrace();
        }



	}

}
